package com.qufenqi.pay.abs.common.utils;

 


import com.qufenqi.pay.abs.common.enumeration.FileType;

import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;

/** 
 * 文件类型判断类 
 */  
public class FileTypeJudge {  
 
    /** 
     * 将文件头转换成16进制字符串 
     *  
     * @param 原生byte 
     * @return 16进制字符串 
     */  
    private static String bytesToHexString(byte[] src){  
          
        StringBuilder stringBuilder = new StringBuilder();     
        if (src == null || src.length <= 0) {     
            return null;     
        }     
        for (int i = 0; i < src.length; i++) {     
            int v = src[i] & 0xFF;     
            String hv = Integer.toHexString(v);     
            if (hv.length() < 2) {     
                stringBuilder.append(0);     
            }     
            stringBuilder.append(hv);     
        }     
        return stringBuilder.toString();     
    }  
     
    /** 
     * 得到文件头 
     *  
     * @param inputStream 文件输入流 
     * @return 文件头 
     * @throws IOException 
     */  
    private static String getFileContent(InputStream inputStream) throws IOException {  
          
        byte[] b = new byte[28];          
        try {  
            if(inputStream == null){
            	return null;
            }  
            inputStream.read(b, 0, 28);  
        } catch (IOException e) {  
            e.printStackTrace();  
            throw e;  
        } finally {  
            if (inputStream != null) {  
                try {
                    inputStream.close();
                } catch (IOException e) {
                    e.printStackTrace();
                    throw e;
                }
            }  
        }  
        return bytesToHexString(b);  
    }  
      
    /** 
     * 判断文件类型 
     *  
     * @param filePath 文件路径 
     * @return 文件类型 
     */  
    public static FileType getType(InputStream inputStream) throws IOException {
          
        String fileHead = getFileContent(inputStream);  
          
        if (fileHead == null || fileHead.length() == 0) {  
            return null;  
        }  
          
        fileHead = fileHead.toUpperCase();  
          
        FileType[] fileTypes = FileType.values();
          
        for (FileType type : fileTypes) {  
            if (fileHead.startsWith(type.getValue())) {  
                return type;  
            }  
        }  
          
        return null;  
    }



    /** <Description functions in a word>
     * Bytes to Hex String
     * 将字节数组转换成16进制字符串
     * <Detail description>
     * @author  Qiuzhenping
     * @param src
     * @return [Parameters description]
     * @return String [Return type description]
     * @exception throws [Exception] [Exception description]
     * @see [Related classes#Related methods#Related properties]
     */
    public static String bytes2HexString(byte[] src) {
        StringBuilder stringBuilder = new StringBuilder();
        if (src == null || src.length <= 0) {
            return null;
        }
        for (int i = 0; i < src.length; i++) {
            int v = src[i] & 0xFF;
            String hv = Integer.toHexString(v);
            if (hv.length() < 2) {
                stringBuilder.append(0);
            }
            stringBuilder.append(hv);
        }
        System.out.println(" bytes2HexString = "+stringBuilder.toString().toUpperCase());
        return stringBuilder.toString().toUpperCase();
    }

    /** <Description functions in a word>
     * Judge this FileInputStream is csv file
     * 判断该文件流头部是否包含有指定的信息，从而确认该文件是不是正确的文件类型
     * <Detail description>
     * @author  Qiuzhenping
     * @param is
     * @return [Parameters description]
     * @return boolean [Return type description]
     * @exception throws [Exception] [Exception description]
     * @see [Related classes#Related methods#Related properties]
     */
    public static boolean judgeIsCSV(InputStream is){
        try {
            byte[] b = new byte[4];
            is.read(b, 0, b.length);
            return bytes2HexString(b).contains("5B75726C");//CSV文件的头部的前4个字节
        } catch (Exception e) {
            e.printStackTrace();
        }
        return false;
    }
}  
